<?php

	$cache 	  = true;
	$cachedir = dirname(__FILE__) . '/combine';
	$cssdir   = dirname(__FILE__) . '/../public/static/css';
	$jsdir    = dirname(__FILE__) . '/../public/static/javascript';

    $contents = '';

//    var_dump($argv);
//    exit();
	// Determine the directory and type we should use
	switch ($argv[1]) {
		case 'css':
			$base = realpath($cssdir);
            $cachefile = 'combined.css';
			break;
		case 'js':
			$base = realpath($jsdir);
            $cachefile = 'combined.js';
			break;
		default:
			header ("HTTP/1.0 503 Not Implemented");
			exit;
	};

	$type = $argv[1];
    unset($argv[0]);
    unset($argv[1]);
	$elements = $argv;

    // Get contents of the files
    reset($elements);
    foreach($elements as $element) {
        $path = realpath($base . '/' . $element);
        $contents .= file_get_contents($path) . "\n";
    }

    if($type == 'css') {
        $contents = str_replace('@CHARSET "UTF-8";', '', $contents);
        $contents = '@CHARSET "UTF-8";' . "\n" . $contents;
    }

    // Store cache
    if ($cache) {
        if ($fp = fopen($cachedir . '/' . $cachefile, 'wb')) {
            fwrite($fp, $contents);
            fclose($fp);
        }
    }
